package com.example.librarymanagementsystembackend.borrowingmanagement.controller;

import com.example.librarymanagementsystembackend.borrowingmanagement.entity.BorrowBooksDTO;
import com.example.librarymanagementsystembackend.borrowingmanagement.entity.BorrowRecord;
import com.example.librarymanagementsystembackend.borrowingmanagement.entity.CategoryBorrowNumber;
import com.example.librarymanagementsystembackend.borrowingmanagement.entity.ReturnBooksDTO;
import com.example.librarymanagementsystembackend.borrowingmanagement.service.BorrowingRecordService;
import com.example.librarymanagementsystembackend.utils.InvalidRequestException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 借阅管理模块 控制器
 */
@RestController
@RequestMapping("/borrowing")
public class BorrowingRecordController {

    // 假设的服务层接口，实际应用中应该替换为真实的业务逻辑
    @Autowired
    private BorrowingRecordService borrowingRecordService;

    /**
     * 获取图书类别借阅比例（饼图数据）
     */
    @GetMapping("/category")
    public List<CategoryBorrowNumber> getCategoryBorrowNumber() {
        return new ArrayList<>();
    }


    /**
     * 借书服务
     * <p>
     * 示例数据
     * {
     * "userId" : 8,
     * "bookId": 16,
     * "borrowTime" : "2025-10-01 10:20:10"
     * }
     *
     * @param borrowBooksDTO 借书信息
     * @return 借阅记录ID
     */
    @PostMapping("/borrowBook")
    public Integer borrowBook(@RequestBody BorrowBooksDTO borrowBooksDTO) {
        try {
            return borrowingRecordService.borrowBook(borrowBooksDTO);
        } catch (InvalidRequestException e) {
            throw new InvalidRequestException("借书失败(" + e.getMessage() + ")");
        }
    }

    /**
     * 还书服务
     * <p>
     * 示例数据
     * {
     *

     * "borrowTime" : "2025-10-05 10:20:10"
     * }
     *
     * @param returnBooksDTO
     * @return 逾期费用
     */
    @PutMapping("/returnBook")
    public BigDecimal returnBook(@RequestBody ReturnBooksDTO returnBooksDTO) {
        try {
            return borrowingRecordService.returnBook(returnBooksDTO.getBorrowId(), returnBooksDTO.getReturnTime());
        } catch (InvalidRequestException e) {
            throw new InvalidRequestException("还书失败(" + e.getMessage() + ")");
        }
    }

    /**
     * 获取用户借阅记录通过 ID
     */
    @GetMapping("/record/{userId}")
    public List<BorrowRecord> getBorrowRecordsByUserId(@PathVariable Integer userId) {
        return borrowingRecordService.getBorrowRecordsByUserId(userId);
    }

    /**
     * 获取所有用户借阅记录
     */
    @GetMapping("/record/all")
    public List<BorrowRecord> getAllBorrowingRecords() {
        return borrowingRecordService.getAllBorrowingRecords();
    }
}
